# -*- coding: utf-8 -*-

'''
Created on 09-07-10

@author: peterd
'''




from web.service.LBCParser import LBCParser
from geo.models import NoSuchCity, NoSuchStreet, NoSuchLocation
from infra.logging import logger
from geo.models import NoSuchCity, NoSuchStreet, NoSuchLocation


class DoctorParser(LBCParser):
	'''
	classdocs
	'''

	def __init__(self, lbcategory, dataProcessor): 
		LBCParser.__init__(self, lbcategory, dataProcessor)
		
		self.inGDList_ = False
		self.inGD_ = False
		self.matched_ = False
		self.inUrl_ = False
		self.url_ = None
		self.inName_ = False
		self.name_ = None
		self.ln_ = 0
				
		log = logger.getLogger()
		log.debug("%s = %s" % (self, self.__dict__))
		
		
	def reset__(self):
		self.name_ = None
		self.ln_ = 0
		
		LBCParser.reset__(self)


	def start_div(self, attrs):
		for attr, value in attrs:
			if attr == 'id' and value == 'mainContent':
				self.inGDList_ = True
				break
					
					
	def end_div(self):
		if self.inGDList_:
			self.inGDList_ = False
			
			
	def start_p(self, attrs):
		if self.inGDList_:
			self.inGD_ = True
			
			
	def end_p(self):
		if self.inGD_:
			self.inGD_ = False
			self.reset__()
			
			
	def start_a(self, attrs):
		for attr, value in attrs:
			if self.inGD_ and attr == 'href':
				self.inUrl_ = True
				self.url_ = self.dataProcessor_.getUrl(value)
				break		
			
			
	def end_a(self):
		if self.inUrl_:
			self.inUrl_ = False
			
			
	def start_b(self, attrs):
		if self.inGD_:
			self.inName_ = True
			

	def end_b(self):
		if self.inName_:
			self.inName_ = False	
			
			
	def start_br(self, attrs):
		if self.inGD_:
			self.ln_ += 1
			
			
			
	def do_handle_data(self, data):
		log = logger.getLogger()
		
		if self.inName_:
			self.name_ = data
						
		if self.ln_ == 1:
			self.matched_ = self.dataProcessor_.match(data)
			
		if self.ln_ == 2 and self.matched_:
			locContent = data.split(',')		
			numElem = len(locContent)
			
			try:				
				self.setCity(data)
				self.setStreet(data)

				if len(locContent) > 2:
					self.building_ = ''.join(locContent[0:numElem - 2])
															
				if self.name_:
					self.createOrganization(name = self.name_, url = self.url_)
								
			except NoSuchCity, e:
				log.error('NoSuchCity exception caught, reason: %s' % e)						
			except NoSuchStreet, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)
			except NoSuchLocation, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)	
			
			
	def getId(self):
		return self.__class__.__name__
	
	
	
class AmbulanceParser(LBCParser):

	def __init__(self, lbcategory, dataProcessor): 
		LBCParser.__init__(self, lbcategory, dataProcessor)
		
		self.inGDList_ = False
		self.inGD_ = False
		self.matched_ = False
		self.inUrl_ = False
		self.url_ = None
		self.inName_ = False
		self.name_ = None
		self.ln_ = 0
				
		log = logger.getLogger()
		log.debug("%s = %s" % (self, self.__dict__))
		
		
	def reset__(self):
		self.name_ = None
		self.url_ = None
		self.ln_ = 0
		
		LBCParser.reset__(self)


	def start_div(self, attrs):
		for attr, value in attrs:
			if attr == 'id' and value == 'mainContent':
				self.inGDList_ = True
				break
					
					
	def end_div(self):
		if self.inGDList_:
			self.inGDList_ = False
			

	def start_a(self, attrs):
		for attr, value in attrs:
			if self.inGDList_ and attr == 'href' and (value.find('profil') != -1 or value.find('zariadenie') != -1):
				self.inUrl_ = True
				self.url_ = self.dataProcessor_.getUrl(value)
				break
			
			
	def end_a(self):
		if self.inUrl_:
			self.inUrl_ = False
			

	def start_br(self, attrs):
		if self.url_:
			self.ln_ += 1
			
			
	def do_handle_data(self, data):
		log = logger.getLogger()
				
		if self.inUrl_:
			self.name_ = data
			self.matched_ = self.dataProcessor_.match(self.name_)
			
			if not self.matched_:
				self.reset__()
				
			
		if self.ln_ == 1 and self.matched_:
			locContent = data.split(',')		
			numElem = len(locContent)
			
			try:					
				self.setCity(data)
				self.setStreet(data)

				if len(locContent) > 2:
					self.building_ = ''.join(locContent[0:numElem - 2])	
					
							
				if self.name_:
					self.createOrganization(name = self.name_, url = self.url_)
								
			except NoSuchCity, e:
				log.error('NoSuchCity exception caught, reason: %s' % e)						
			except NoSuchStreet, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)
			except NoSuchLocation, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)
			
			self.reset__()
			
						
	def getId(self):
		return self.__class__.__name__
	
	
	
class PharmacyParser(LBCParser):
	'''
	classdocs
	'''

	def __init__(self, lbcategory, dataProcessor): 
		LBCParser.__init__(self, lbcategory, dataProcessor)

		self.inGDList_ = False
		self.inGD_ = False
		self.inUrl_ = False
		self.url_ = None
		self.inName_ = False
		self.name_ = None
		self.ln_ = 0
				
		log = logger.getLogger()
		log.debug("%s = %s" % (self, self.__dict__))
		
		
	def reset__(self):
		self.name_ = None
		self.ln_ = 0
		
		LBCParser.reset__(self)


	def start_div(self, attrs):
		for attr, value in attrs:
			if attr == 'id' and value == 'mainContent':
				self.inGDList_ = True
				break
					
					
	def end_div(self):
		if self.inGDList_:
			self.inGDList_ = False
			
			
	def start_p(self, attrs):
		if self.inGDList_:
			self.inGD_ = True
			
			
	def end_p(self):
		if self.inGD_:
			self.inGD_ = False
			self.reset__()
			
			
	def start_a(self, attrs):
		for attr, value in attrs:
			if self.inGD_ and attr == 'href':
				self.inUrl_ = True
				self.url_ = self.dataProcessor_.getUrl(value)
				break		
			
			
	def end_a(self):
		if self.inUrl_:
			self.inUrl_ = False
			
			
	def start_b(self, attrs):
		if self.inGD_:
			self.inName_ = True
			

	def end_b(self):
		if self.inName_:
			self.inName_ = False	
			
			
	def start_br(self, attrs):
		if self.inGD_:
			self.ln_ += 1
			
			
			
	def do_handle_data(self, data):
		
		log = logger.getLogger()
		
		if self.inName_:
			self.name_ = data
						
		if self.ln_ == 1:
			locContent = data.split(',')		
			numElem = len(locContent)
			
			try:
				self.setCity(data)
				self.setStreet(data)

				if len(locContent) > 2:
					self.building_ = ''.join(locContent[0:numElem - 2])	
							
				if self.name_:
					self.createOrganization(name = self.name_, url = self.url_)
								
			except NoSuchCity, e:
				log.error('NoSuchCity exception caught, reason: %s' % e)						
			except NoSuchStreet, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)
			except NoSuchLocation, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)	
			
			
	def getId(self):
		return self.__class__.__name__	
	
	

#
# utils
#

# TODO:
#http://www.zzz.sk/?profil=567
#http://www.zzz.sk?register=770

def getCorId(url):
	corId = None
	'''
	# url such as: 
	# www.obedovat.sk/bratislava/restaurant.php?id=5542&show=detaily
	# www.obedovat.sk/bratislava/photos.php?id=5542&albid=1581
	m = re.search('\.*\?id=([0-9]+)', url)
	if m:
		corId = m.group(1)
	else:
		# url such as:
		# www.obedovat.sk/bratislava/Restauracia-detaily_5542_Pizzeria_Agnezo?charset=1
		m = re.search('_([0-9]+)_', url)
		if m:
			corId = m.group(1)
	'''
	return corId